// Source : https://leetcode.com/problems/3sum/
// Author : simba
// Date   : 2016-06-07

package main

import (
	"fmt"
	"sort"
)

func threeSum(nums []int) [][]int {
	var result [][]int
	m := make(map[[3]int]struct{})

	for i := range nums {
		for j := i + 1; j <= len(nums)-2; j++ {
			for k := j + 1; k <= len(nums)-1; k++ {
				if nums[i]+nums[j]+nums[k] == 0 {
					s := []int{nums[i], nums[j], nums[k]}
					sort.Sort(sort.IntSlice(s))
					a := [3]int{s[0], s[1], s[2]}
					if _, ok := m[a]; !ok {
						m[a] = struct{}{}
						result = append(result, s)
					}
				}
			}
		}
	}
	return result
}

func main() {
	//固定测试用例,正确答案
	//	[[-1 0 1][-1 -1 2]]

	s1 := []int{-1, 0, 1, 2, -1, -4}
	a1 := threeSum(s1)
	fmt.Println("数组", s1)
	fmt.Println("输出答案", a1)
	fmt.Println("正确答案[[-1 0 1] [-1 -1 2]]")
}
